﻿Imports System
Imports IDServer
Imports System.Threading.Thread
Imports System.Text.RegularExpressions
Imports System.IO
Imports System.String
Imports Microsoft.VisualBasic
Imports Microsoft.VisualBasic.VBMath
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports CalendarColumn
Imports System.Configuration

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Public Class Frm_Account
    Implements IDServer.IDServer.IDServer

    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Public mDS As DataSet  '新增表單的的資料集
    Private dsBSC As DataSet  '存放公共資料
    Private ds As New DataSet  '瀏覽表單的資料集
    Private dsTemp As New DataSet '更新剩餘數量及剩餘金額時用
    Private strOriginalNum As String = ""
    Private tempDS As New DataSet
    Private bFind As Boolean
    Private IsCheck As Boolean = True  '是否檢查發票數量、金額的合法性
    Private Fields As String
    Private WithEvents MainFrom As Frm_MainForm

    Private appPath As String = Application.StartupPath & "\Pic\" '圖像文件路徑
    Private Img As ImageList '圖像列表
    Private SelectNode As TreeNode
    Private dsAccount As New DataSet

    Private CaptionStr As String = "科目代碼^FNumber&科目名稱^FName&科目名稱(中文)^FName_Cht" & _
                                   "&借貸方向^FDC&助記碼^FHelperCode&是否禁用^FDelete&全名^FFullName"

    Private strSeparating As String = "" '會計科目分隔字元串
    Private FParentAccount As String '上級會計科目

    Private strBillstatusID = ""
    Private mPoint As Point
    Private X, Y As Integer

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainFrom
        End Get
        Set(ByVal value As Frm_MainForm)
            MainFrom = value
        End Set
    End Property

#Region "inteface define"
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd
        Try
            If AddEnable = False Then
                MessageBox.Show(My.resources.NotAllowAdd, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If

            Me.Cursor = Cursors.WaitCursor

            If Me.tvAccount.Nodes.Count = 0 Then
                Filter()
            End If

            ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd
            EnabledControls(True)
            Me.TabControl1.SelectedIndex = 1
            ClearText()
            SetButton(ButtonType.Add, False)
            Me.TxtFNumber.Focus()

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel
        Try
            ''Dim strCode As String
            ClearText()
            EnabledControls(False)

            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp

            If dsBSC Is Nothing Then
                Exit Function
            End If


            If dsBSC.Tables.Count = 0 Then
                Exit Function
            End If

            If lvAccount.SelectedItems.Count > 0 Then
                If lvAccount.SelectedItems IsNot Nothing Then
                    Dim FNumber As String
                    FNumber = lvAccount.SelectedItems(0).Text
                    If Me.TabControl1.SelectedIndex = 1 Then
                        FillText(FNumber)
                    End If


                End If

            End If


            IsFind = FindRecord()
            SetButton(ButtonType.Cancel, IsFind)

            GetToolEnable()

        Catch ex As Exception
            MsgBox("ActionCancel:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete
        If DeleteEnable = False Then
            MessageBox.Show(My.Resources.NotAllowDelete, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

        Dim cnSqlServer As New SqlConnection
        cnSqlServer = GetConn()
        Dim cmdSqlServer As New SqlCommand
        Dim trnSqlServer As SqlTransaction = cnSqlServer.BeginTransaction
        Dim FNumber, FName_Cht As String
        Dim FAccountID, FDetail As Integer
        Dim sql As String
        Dim selIndex As Integer = 0

        Try
            If dsBSC Is Nothing Then
                Exit Function
            End If

            If dsBSC.Tables("t_Account").Rows.Count = 0 Then
                Exit Function
            End If

            If lvAccount.SelectedItems IsNot Nothing Then
                FNumber = lvAccount.SelectedItems(0).Text
                FName_Cht = lvAccount.SelectedItems(0).SubItems(2).Text
                Dim Rows() As DataRow = dsBSC.Tables("t_Account").Select("FNumber='" & FNumber & "'")
                For Each Row As DataRow In Rows
                    FAccountID = Row.Item("FAccountID")

                Next
                selIndex = lvAccount.SelectedItems(0).Index

            End If

            '檢查是否有下級科目
            If dsBSC.Tables("t_Account").Compute("COUNT(FParentID)", "FParentID='" & FAccountID & "'") > 0 Then
                trnSqlServer.Rollback()
                MsgBox(GetMsg("BSC", "Exists Subordinate Account"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Exit Function
            End If

            If MsgBox("確實要刪除科目：" & FName_Cht & "嗎？", MsgBoxStyle.Question Or MessageBoxButtons.YesNo, "資訊提示") = MsgBoxResult.Yes Then
                With cmdSqlServer
                    .Connection = cnSqlServer
                    .Transaction = trnSqlServer
                    .CommandType = CommandType.Text

                    '刪除記錄
                    .CommandText = "DELETE t_Account  WHERE FNumber='" & FNumber & "'"
                    .ExecuteNonQuery()



                    '如果明細已經全部刪除，則需將上級科目的FDetail改為1



                    strSeparating = dsBSC.Tables("System").Rows(0).Item(0)

                    If FNumber.IndexOf(strSeparating) > 0 Then
                        FParentAccount = FNumber.Substring(0, Microsoft.VisualBasic.InStrRev(FNumber, strSeparating) - 1)
                        sql = "SELECT COUNT(*) FROM t_Account WHERE LEN(FNumber)>LEN('" & FParentAccount & "') AND LEFT(FNumber,LEN('" & FParentAccount & "'))='" & FParentAccount & "'"
                        .CommandText = sql
                        FDetail = .ExecuteScalar()

                        If FDetail = 0 Then
                            .CommandText = "UPDATE t_Account SET FDetail=1 WHERE FNumber='" & FParentAccount & "'"
                            .ExecuteNonQuery()

                        End If
                    End If


                End With
                trnSqlServer.Commit()


                '清空dsbsc中相關記錄
                Dim rows() As DataRow = dsBSC.Tables("t_Account").Select("FNumber='" & FNumber & "'")
                If rows.Length > 0 Then
                    For Each row As DataRow In rows
                        row.Delete()

                    Next
                End If

                dsBSC.AcceptChanges()

                If selIndex < lvAccount.Items.Count Then
                    lvAccount.Items(selIndex).Selected = True
                End If


                IsFind = FindRecord()
                SetButton(ButtonType.Browse, IsFind)
                lvAccount.SelectedItems(0).Remove()

                MsgBox("刪除成功！", MsgBoxStyle.OkOnly, "資訊提示")

            Else
                trnSqlServer.Rollback()


            End If



        Catch ex As Exception
            trnSqlServer.Rollback()
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)



        End Try
    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit
        Try
            If EditEnable = False Then
                MessageBox.Show(My.resources.NotAllowEidt, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
                Exit Function
            End If


            If dsBSC Is Nothing Then
                Exit Function
            End If

            If dsBSC.Tables("t_Account").Rows.Count = 0 Then
                Exit Function
            End If

            If lvAccount.SelectedItems.Count = 0 Then
                Exit Function
            End If

            If Me.TabControl1.SelectedIndex = 0 Then
                Me.TabControl1.SelectedIndex = 1
            End If



            If lvAccount.SelectedItems.Count > 0 Then

                Dim FNumber As String
                FNumber = lvAccount.SelectedItems(0).Text
                FillText(FNumber)

                strOriginalNum = FNumber
                'Dim strFilter As String
                'strFilter = "FNumber='" & FNumber & "'"
                ''獲取FAccountID
                'Dim rows() As DataRow = dsBSC.Tables("t_Account").Select(strFilter)
                'For Each row As DataRow In rows
                '    strOriginalNum = row.Item("FAccountID")

                'Next

            End If

            ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit
            EnabledControls(True)
            SetButton(ButtonType.Edit, False)


        Catch ex As Exception
            MsgBox("ActionEdit:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport
        Try
            If App.ExportEnable = False Then
                MessageBox.Show(My.Resources.NotAllowExport, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If

            Me.Cursor = Cursors.WaitCursor
            If FindRecord() = False Then
                Exit Function
            Else

                Dim tb As DataTable = dsBSC.Tables("t_Account").Copy
                Dim dsTemp As New DataSet

                tb.Columns("faccountid").Caption = "科目內碼"
                tb.Columns("fnumber").Caption = "科目代碼"
                tb.Columns("fname").Caption = "科目名稱"
                tb.Columns("fname_cht").Caption = "科目名稱(中文)"
                tb.Columns("flevel").Caption = "科目級次"
                tb.Columns("fdetail").Caption = "明細科目"
                tb.Columns("fparentid").Caption = "上級科目內碼"
                tb.Columns("frootid").Caption = "上級科目內碼"
                tb.Columns("fgroupid").Caption = "科目類別內碼"
                tb.Columns("fdc").Caption = "借貸方向"
                tb.Columns("fhelpercode").Caption = "助記碼"
                tb.Columns("fcurrencyid").Caption = "幣別內碼"
                tb.Columns("fadjustrate").Caption = "期末調匯"
                tb.Columns("fquantities").Caption = "數量輔助核算"
                tb.Columns("fiscash").Caption = "現金類科目"
                tb.Columns("fisbank").Caption = "銀行類科目"
                tb.Columns("fjournal").Caption = "輸出明細賬"
                tb.Columns("fcontact").Caption = "往來核算"
                tb.Columns("fiscashflow").Caption = "現金等價物"
                tb.Columns("fcheckcase").Caption = "核算項目"
                tb.Columns("fisbudget").Caption = "是否預算科目"
                tb.Columns("facctint").Caption = "是否計息"
                tb.Columns("fintrate").Caption = "利息率"
                tb.Columns("fdelete").Caption = "是否禁用"
                tb.Columns("ffullname").Caption = "全名"

                tb.Columns.Remove("fgroupid2")

                dsTemp.Tables.Add(tb)
                dsTemp.AcceptChanges()

                '將表名改為中文名
                dsTemp.Tables(0).TableName = "會計科目"

                SaveXls(dsTemp)

            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst
        Try
            If dsbsc Is Nothing Then
                Exit Function
            End If

            If dsbsc.Tables.Count = 0 Then
                Exit Function
            End If

            If dsbsc.Tables(0).Rows.Count = 0 Then
                Exit Function
            End If


            If ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp Then
                If lvAccount.Items IsNot Nothing Then
                    Dim FNumber As String
                    lvAccount.Focus()
                    lvAccount.Items(0).Selected = True
                    If Me.TabControl1.SelectedIndex = 1 Then
                        FNumber = lvAccount.SelectedItems(0).Text
                        FillText(FNumber)

                    End If

                End If

            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast
        Try
            If dsBSC Is Nothing Then
                Exit Function
            End If

            If dsBSC.Tables.Count = 0 Then
                Exit Function
            End If

            If dsBSC.Tables(0).Rows.Count = 0 Then
                Exit Function
            End If

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp Then
                If lvAccount.Items IsNot Nothing Then
                    Dim FNumber As String
                    lvAccount.Focus()
                    lvAccount.Items(lvAccount.Items.Count - 1).Selected = True
                    If Me.TabControl1.SelectedIndex = 1 Then
                        FNumber = lvAccount.SelectedItems(0).Text
                        FillText(FNumber)

                    End If
                End If

            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext
        Try
            If dsBSC Is Nothing Then
                Exit Function
            End If

            If dsBSC.Tables.Count = 0 Then
                Exit Function
            End If

            If dsBSC.Tables(0).Rows.Count = 0 Then
                Exit Function
            End If

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp Then
                If lvAccount.Items IsNot Nothing Then
                    Dim FNumber As String
                    Dim intRow As Integer
                    intRow = lvAccount.SelectedItems(0).Index
                    If lvAccount.SelectedItems(0).Index < lvAccount.Items.Count - 1 Then
                        lvAccount.Focus()
                        Me.lvAccount.Items(intRow + 1).Selected = True

                        If Me.TabControl1.SelectedIndex = 1 Then
                            FNumber = lvAccount.SelectedItems(0).Text
                            FillText(FNumber)

                        End If
                    End If
                End If

            End If


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious
        Try
            If dsBSC Is Nothing Then
                Exit Function
            End If

            If dsBSC.Tables.Count = 0 Then
                Exit Function
            End If

            If dsBSC.Tables(0).Rows.Count = 0 Then
                Exit Function
            End If

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp Then
                If lvAccount.Items IsNot Nothing Then
                    Dim FNumber As String
                    Dim intRow As Integer
                    intRow = lvAccount.SelectedItems(0).Index
                    If lvAccount.SelectedItems(0).Index > 0 Then
                        lvAccount.Focus()

                        Me.lvAccount.Items(intRow - 1).Selected = True
                        If Me.TabControl1.SelectedIndex = 1 Then
                            FNumber = lvAccount.SelectedItems(0).Text
                            FillText(FNumber)

                        End If
                    End If
                End If

            End If


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize
        Try

            Dim strSql As String
            Dim FNumber As String = ""
            strSeparating = dsBSC.Tables("System").Rows(0).Item(0)

            '科目代碼
            If Me.TxtFNumber.Text.Length <= 0 Then
                MsgBox(GetMsg("BSC", "NotNull"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.TxtFNumber.Focus()
                Return False
            End If

            '檢查科目代碼是否重復
            FNumber = Me.TxtFNumber.Text & ""

            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then
                strSql = "SELECT COUNT(*) FROM dbo.t_Account WHERE FNumber='" & FNumber & "'"
                If CInt(GetValue(strSql)) > 0 Then
                    MsgBox(GetMsg("BSC", "FNumberIsReplace"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.TxtFNumber.Focus()
                    Return False
                End If
            ElseIf ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                strSql = "SELECT COUNT(*) FROM dbo.t_Account WHERE FNumber='" & FNumber & "' AND FNumber<>'" & strOriginalNum & "'"
                If CInt(GetValue(strSql)) > 0 Then
                    MsgBox(GetMsg("BSC", "FNumberIsReplace"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.TxtFNumber.Focus()
                    Return False
                End If
            End If

            '檢查科目的合法性
            If FNumber.Split(strSeparating).Length - 1 > 0 Then
                FParentAccount = FNumber.Substring(0, Microsoft.VisualBasic.InStrRev(FNumber, strSeparating) - 1)
                strSql = "SELECT COUNT(*) FROM dbo.t_Account WHERE FNumber='" & FParentAccount & "'"
                If CInt(GetValue(strSql)) = 0 Then
                    MsgBox(GetMsg("BSC", "NotHigherAccount"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.TxtFNumber.Focus()
                    Return False
                End If
            End If

            Dim reg As Regex
            If strSeparating = "." Then
                reg = New Regex("^\d+$|^\d+(\.\d+){1,}$")
            ElseIf strSeparating = "-" Then
                reg = New Regex("^\d+$|^\d+(-\d+){1,}$")

            End If

            'Dim reg As New Regex("^\d+$|^\d+(-\d+){1,}$|^\d+(\.\d+){1,}$")
            If reg.IsMatch(FNumber) = False Then
                MsgBox(GetMsg("BSC", "NotValidAccount"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.TxtFNumber.Focus()
                Return False
            End If


            '餘額方向不能為空
            If Me.cmbFDC.Text.Length <= 0 Then
                MsgBox(GetMsg("BSC", "NotNull"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.cmbFDC.Focus()
                Return False
            End If

            '科目名稱
            If Me.txtFName.Text.Length <= 0 Then
                MsgBox(GetMsg("BSC", "NotNull"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtFName.Focus()
                Return False
            End If

            '科目類別不能為空
            If Me.cmbFGroupID.Text.Length <= 0 Then
                MsgBox(GetMsg("BSC", "NotNull"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.cmbFGroupID.Focus()
                Return False
            End If

            '科目名稱(中文)
            If Me.txtFName_Cht.Text.Length <= 0 Then
                MsgBox(GetMsg("BSC", "NotNull"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Me.txtFName_Cht.Focus()
                Return False
            End If

            '檢查科目利息
            If Me.chkFAcctint.Checked = True Then
                If Me.nudFintRate.Value = 0 Then
                    MsgBox(GetMsg("PUR", "NumberIsZero"), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                    Me.nudFintRate.Focus()
                    Return False
                End If
            End If

            Return True
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        Finally
            Me.Cursor = Cursors.Default
        End Try
    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave
        Try
            If ActionInitialize() = False Then Exit Function

            If BtnActionSave() = True Then
                IsFind = FindRecord()
                SetButton(ButtonType.Browse, IsFind)
                GetToolEnable()
                Return True
            Else

                'IsFind = FindRecord()
                'SetButton(ButtonType.Browse, IsFind)
                Return False
            End If
        Catch ex As Exception
            MsgBox("ActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect
        Filter()
    End Function

    ''' <summary>
    ''' 核准單據
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionChecked() As Boolean
        If CheckEnable = False Then
            MessageBox.Show("對不起，您沒有審核權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

    End Function

    ''' <summary>
    ''' 反核准單據
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function ActionUnChecked() As Boolean
        If CheckEnable = False Then
            MessageBox.Show("對不起，您沒有反審核權限", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            Exit Function
        End If

    End Function

#End Region

#Region "Windows Sub"
    Private Sub Frm_Account_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try

            Me.mnuUnUse.Image = Image.FromFile(appPath & "FDelete.bmp")
            Me.mnuUse.Image = Image.FromFile(appPath & "UnFDelete.bmp")

            With Me.lvAccount
                .AllowColumnReorder = True
                .GridLines = True
                .FullRowSelect = True

            End With

            With Me.tvAccount
                .FullRowSelect = True
            End With

            LoadlvColumns(lvAccount)
            EnabledControls(False)
            '' LoadMultiLanguage()
            'Filter()

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub mnuExpand_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExpand.Click
        If Me.tvAccount.SelectedNode IsNot Nothing Then
            Me.tvAccount.SelectedNode.Expand()
        End If

    End Sub

    Private Sub mnuCollapse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCollapse.Click

        If Me.tvAccount.SelectedNode IsNot Nothing Then
            Me.tvAccount.SelectedNode.Collapse()
        End If
    End Sub

    Private Sub mnuExpandAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExpandAll.Click

        If Me.tvAccount.SelectedNode IsNot Nothing Then
            Me.tvAccount.ExpandAll()
        End If
    End Sub

    Private Sub mnuCollapseAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuCollapseAll.Click

        If Me.tvAccount.SelectedNode IsNot Nothing Then
            Me.tvAccount.CollapseAll()
        End If
    End Sub

    Private Sub tvAccount_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvAccount.AfterSelect
        Try
            If tvAccount.SelectedNode Is Nothing Then
                Exit Sub
            End If
            If tvAccount.SelectedNode.Level = 0 Then
                ListAccount("Account")
            ElseIf tvAccount.SelectedNode.Level = 1 Then
                ListAccount(tvAccount.SelectedNode.Tag.ToString.Split("^")(0))
            Else
                ListAccount(tvAccount.SelectedNode.Text.Substring(1, tvAccount.SelectedNode.Text.IndexOf(")") - 1))

            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub dgrd1_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs)
        Try

        Catch ex As Exception
            Exit Sub
        End Try
    End Sub

    Private Sub TxtFNumber_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles nudFintRate.KeyDown, _
             cmbFDC.KeyDown, TxtFHelperCode.KeyDown, chkFIsBudget.KeyDown, chkFAcctint.KeyDown, chkFIsCashFlow.KeyDown, _
             chkFIsBank.KeyDown, chkFIsCash.KeyDown, chkFQuantities.KeyDown, chkFJournal.KeyDown, chkFContact.KeyDown, _
             chkFAdjustRate.KeyDown, cmbFCurrencyID.KeyDown, cmbFGroupID.KeyDown, txtFName_Cht.KeyDown, txtFName.KeyDown, TxtFNumber.KeyDown
        Try
            If e.KeyCode = Keys.Shift Then
                e.Handled = True
                If e.KeyCode = Keys.Enter Then
                    SendKeys.Send("+{TAB}")
                    SendKeys.Send("{F4}")
                End If
            Else
                Select Case e.KeyCode
                    Case Keys.Enter
                        e.Handled = True
                        SendKeys.Send("{TAB}")
                        SendKeys.Send("{F4}")
                End Select
            End If
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub chkFAcctint_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkFAcctint.CheckedChanged
        Try
            Me.nudFintRate.Enabled = Me.chkFAcctint.Checked
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub lvAccount_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvAccount.DoubleClick
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Exit Sub
            End If

            If dsBSC.Tables.Count = 0 Then
                Exit Sub
            End If

            If dsBSC.Tables("t_Account").Rows.Count = 0 Then
                Exit Sub
            End If

            If lvAccount.SelectedItems.Count > 0 Then
                If lvAccount.SelectedItems IsNot Nothing Then
                    Dim FNumber As String
                    FNumber = lvAccount.SelectedItems(0).Text
                    FillText(FNumber)
                    Me.TabControl1.SelectedIndex = 1

                End If
            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub mnuUnUse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUnUse.Click
        '禁用會計科目
        Dim SQLcn As New SqlConnection
        SQLcn = GetConn()
        Dim SQLCMD As New SqlCommand
        If SQLcn.State <> ConnectionState.Open Then
            SQLcn.Open()

        End If
        Dim trnSqlServer As SqlTransaction = SQLcn.BeginTransaction(IsolationLevel.Serializable)

        '當前選擇的會計科目代碼
        Dim strAccount, FAccount As String


        Try
            If lvAccount.SelectedItems.Count > 0 Then
                strAccount = Me.lvAccount.SelectedItems(0).SubItems(2).Text
                FAccount = Me.lvAccount.SelectedItems(0).SubItems(0).Text
                If MsgBox("確實要禁用科目：" & strAccount & "嗎？", MsgBoxStyle.YesNo, Me.Text) = MsgBoxResult.Yes Then

                    With SQLCMD
                        .Connection = SQLcn
                        .CommandText = "update t_account set FDelete=1  where FNumber='" & FAccount & "'"
                        .Transaction = trnSqlServer
                        .ExecuteNonQuery()

                        Dim selItem As New ListViewItem
                        selItem = Me.lvAccount.SelectedItems(0)
                        selItem.SubItems(5).Text = "是"
                        selItem.ForeColor = Color.Red

                    End With
                    trnSqlServer.Commit()
                End If
            End If
            SQLcn.Close()
        Catch ex As Exception
            trnSqlServer.Rollback()
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        End Try
    End Sub

    Private Sub mnuUse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuUse.Click
        '反禁用會計科目
        Dim SQLcn As New SqlConnection
        SQLcn = GetConn()
        Dim SQLCMD As New SqlCommand
        If SQLcn.State <> ConnectionState.Open Then
            SQLcn.Open()

        End If
        Dim trnSqlServer As SqlTransaction = SQLcn.BeginTransaction(IsolationLevel.Serializable)

        '當前選擇的會計科目代碼
        Dim strAccount, FAccount As String

        Try

            If lvAccount.SelectedItems.Count > 0 Then
                strAccount = Me.lvAccount.SelectedItems(0).SubItems(0).Text
                FAccount = Me.lvAccount.SelectedItems(0).SubItems(0).Text
                With SQLCMD
                    .Connection = SQLcn
                    .CommandText = "update t_account set FDelete=0  where FNumber='" & FAccount & "'"
                    .Transaction = trnSqlServer
                    .ExecuteNonQuery()

                    Dim selItem As New ListViewItem
                    selItem = Me.lvAccount.SelectedItems(0)
                    selItem.SubItems(4).Text = "否"
                    selItem.ForeColor = Color.Black

                End With
                trnSqlServer.Commit()
            End If
            SQLcn.Close()

        Catch ex As Exception

            trnSqlServer.Rollback()
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        End Try
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click, lvFCheckCaseAll.DoubleClick
        Try
            If Me.lvFCheckCaseAll.SelectedItems.Count > 0 Then
                '先將選中項複製到lvFCheckCase
                Dim Index As Integer = 0
                Index = Me.lvFCheckCaseAll.SelectedItems(0).Index
                For I As Integer = 0 To Me.lvFCheckCaseAll.SelectedItems.Count - 1
                    Me.lvFCheckCase.Items.Add(Me.lvFCheckCaseAll.SelectedItems(I).Clone)
                Next

                '移除選中項
                For I As Integer = Me.lvFCheckCaseAll.SelectedItems.Count - 1 To 0 Step -1
                    Me.lvFCheckCaseAll.SelectedItems(I).Remove()

                Next

                If Index < Me.lvFCheckCaseAll.Items.Count Then
                    Me.lvFCheckCaseAll.Focus()
                    Me.lvFCheckCaseAll.Items(Index).Selected = True
                End If

            End If
            CheckItems()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub btnSelectAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectAll.Click
        Try
            If Me.lvFCheckCaseAll.Items.Count > 0 Then
                '先將選中項複製到lvFCheckCase
                For I As Integer = 0 To Me.lvFCheckCaseAll.Items.Count - 1
                    Me.lvFCheckCase.Items.Add(Me.lvFCheckCaseAll.Items(I).Clone)
                Next

                '移除選中項
                For I As Integer = Me.lvFCheckCaseAll.Items.Count - 1 To 0 Step -1
                    Me.lvFCheckCaseAll.Items(I).Remove()

                Next

            End If
            CheckItems()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click, lvFCheckCase.DoubleClick
        Try
            If Me.lvFCheckCase.SelectedItems.Count > 0 Then
                '先將選中項複製到lvFCheckCase
                Dim Index As Integer = 0
                Index = Me.lvFCheckCase.SelectedItems(0).Index
                For I As Integer = 0 To Me.lvFCheckCase.SelectedItems.Count - 1
                    Me.lvFCheckCaseAll.Items.Add(Me.lvFCheckCase.SelectedItems(I).Clone)
                Next

                '移除選中項
                For I As Integer = Me.lvFCheckCase.SelectedItems.Count - 1 To 0 Step -1
                    Me.lvFCheckCase.SelectedItems(I).Remove()

                Next

                If Index < Me.lvFCheckCase.Items.Count Then
                    Me.lvFCheckCase.Focus()
                    Me.lvFCheckCase.Items(Index).Selected = True
                End If

            End If
            CheckItems()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub btnClearAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClearAll.Click
        Try
            If Me.lvFCheckCase.Items.Count > 0 Then
                '先將選中項複製到lvFCheckCase
                For I As Integer = 0 To Me.lvFCheckCase.Items.Count - 1
                    Me.lvFCheckCaseAll.Items.Add(Me.lvFCheckCase.Items(I).Clone)
                Next

                '移除選中項
                For I As Integer = Me.lvFCheckCase.Items.Count - 1 To 0 Step -1
                    Me.lvFCheckCase.Items(I).Remove()

                Next

            End If
            CheckItems()
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Function BtnActionSave() As Boolean
        Dim cnSqlServer As New SqlConnection
        cnSqlServer = GetConn()
        Dim cmdSqlServer As New SqlCommand
        Dim trnSqlServer As SqlTransaction = cnSqlServer.BeginTransaction

        Try
            Dim sql As String = ""
            Dim FNumber As String = ""
            FNumber = Me.TxtFNumber.Text & ""

            '科目名稱、科目名稱（中文）、助記碼
            Dim FName, FName_Cht, FHelperCode, FFullName As String
            FName = Me.txtFName.Text & ""
            FName_Cht = Me.txtFName_Cht.Text & ""
            FHelperCode = Me.TxtFHelperCode.Text & ""
            FFullName = Me.txtFName_Cht.Text & ""


            '新的科目ID
            Dim FAccountID As Integer
            '取值
            FAccountID = GetValue("select ISNULL(max(FAccountID)+1,1000) FROM t_Account")

            '父ID、父節點ID，層次
            Dim FParentID, FRootID, FLevel, FDetail As Integer
            strSeparating = dsBSC.Tables("System").Rows(0).Item(0)
            If FNumber.Split(strSeparating).Length - 1 > 0 Then
                '如果存在上級科目
                FParentAccount = FNumber.Substring(0, Microsoft.VisualBasic.InStrRev(FNumber, strSeparating) - 1)
                Dim strFilter As String
                strFilter = "FNumber='" & FParentAccount & "'"
                '獲取FParentID
                Dim rows() As DataRow = dsBSC.Tables("t_Account").Select(strFilter)
                For Each row As DataRow In rows
                    FParentID = row.Item("FAccountID")
                    FFullName = row.Item("FFullName") & "-" & FFullName
                Next

                '獲取FRootID
                FParentAccount = FNumber.Substring(0, Microsoft.VisualBasic.InStr(FNumber, strSeparating) - 1)
                strFilter = "FNumber='" & FParentAccount & "'"
                rows = dsBSC.Tables("t_Account").Select(strFilter)
                For Each row As DataRow In rows
                    FRootID = row.Item("FAccountID")
                Next

            Else
                '如果不存在上級目錄
                FParentID = 0
                FRootID = FAccountID

            End If

            FLevel = FNumber.Split(strSeparating).Length

            sql = "SELECT COUNT(*) FROM t_Account WHERE LEN(FNumber)>LEN('" & FNumber & "') AND LEFT(FNumber,LEN('" & FNumber & "'))='" & FNumber & "'"
            FDetail = IIf(Val(GetValue(sql)) = 0, 1, 0)

            '科目類別,借貸方向
            Dim FGroupID, FDC As Integer
            FGroupID = CInt(Me.cmbFGroupID.SelectedValue)
            FDC = IIf(Me.cmbFDC.Text = "借", 1, -1)

            '幣別
            Dim FCurrencyID As String
            FCurrencyID = Me.cmbFCurrencyID.SelectedValue

            '期末調匯,現金類科目,輸出明細賬,現金等價物,是否預算科目,數量輔助核算,銀行類科目,往來核算,是否計息
            Dim FAdjustRate, FIsCash, FJournal, FIsCashFlow, FIsBudget, FQuantities, FIsBank, FContact, FAcctint As Integer

            '利息率
            Dim FintRate As Double
            FAdjustRate = IIf(chkFAdjustRate.Checked = True, 1, 0)
            FIsCash = IIf(chkFIsCash.Checked = True, 1, 0)
            FJournal = IIf(chkFJournal.Checked = True, 1, 0)
            FIsCashFlow = IIf(chkFIsCashFlow.Checked = True, 1, 0)
            FIsBudget = IIf(chkFIsBudget.Checked = True, 1, 0)
            FQuantities = IIf(chkFQuantities.Checked = True, 1, 0)
            FIsBank = IIf(chkFIsBank.Checked = True, 1, 0)
            FContact = IIf(chkFContact.Checked = True, 1, 0)
            FAcctint = IIf(chkFAcctint.Checked = True, 1, 0)

            FintRate = Me.nudFintRate.Value

            '核算專案
            Dim FCheckCase, strFCheckCase As String
            FCheckCase = ""
            strFCheckCase = ""
            For I As Integer = 0 To Me.lvFCheckCase.Items.Count - 1
                FCheckCase += Me.lvFCheckCase.Items(I).Tag & ","
                strFCheckCase += Me.lvFCheckCase.Items(I).Text & ","
            Next
            If Me.lvFCheckCase.Items.Count > 0 Then
                FCheckCase = FCheckCase.Substring(0, FCheckCase.Length - 1)
                strFCheckCase = strFCheckCase.Substring(0, strFCheckCase.Length - 1)

            End If


            '當是添加保存
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Then

                With cmdSqlServer
                    .Connection = cnSqlServer
                    .Transaction = trnSqlServer
                    .CommandType = CommandType.Text

                    '向PUR_POA插入記錄
                    .CommandText = "INSERT INTO t_Account (FAccountID, FNumber, FName, FName_Cht, " & _
                                   " FLevel, FDetail, FParentID, FRootID," & _
                                   "  FGroupID, FDC, FHelperCode, FCurrencyID," & _
                                   " FAdjustRate, FQuantities, FIsCash, FIsBank," & _
                                   " FJournal, FContact, FIsCashFlow, FCheckCase, " & _
                                   " FIsBudget,  FAcctint, FintRate,  FDelete, " & _
                                   " FFullName,  BillstatusID, BulidDate, LastUpdateDate, " & _
                                   "BulidComputer, LastUpdateComputer, BulidBy, LastUpdateBy ) VALUES (" & _
                                   FAccountID & ",'" & FNumber & "','" & FName & "','" & FName_Cht & "'," & _
                                   FLevel & "," & FDetail & "," & FParentID & "," & FRootID & "," & _
                                   FGroupID & "," & FDC & ",'" & FHelperCode & "','" & FCurrencyID & "'," & _
                                   FAdjustRate & "," & FQuantities & "," & FIsCash & "," & FIsBank & "," & _
                                   FJournal & "," & FContact & "," & FIsCashFlow & ",'" & FCheckCase & "'," & _
                                   FIsBudget & "," & FAcctint & "," & FintRate & ",0,'" & _
                                   FFullName & "','A'," & _
                                    "convert(varchar(19),getdate(),120),convert(varchar(19),getdate(),120)," & _
                                   "host_name(),host_name(),'" & PubUserName & "','" & PubUserName & "')"
                    .ExecuteNonQuery()

                    '如果是新增的下級目錄，則將上級科目的明細（FDetail）改為1
                    If FNumber.Split(strSeparating).Length - 1 > 0 Then
                        Dim strFilter As String
                        strFilter = "FNumber='" & FParentAccount & "'"
                        '獲取FParentID
                        Dim rows() As DataRow = dsBSC.Tables("t_Account").Select(strFilter)
                        For Each row As DataRow In rows
                            FParentID = row.Item("FAccountID")

                        Next
                        .CommandText = "UPDATE t_Account SET FDetail=0 WHERE FAccountID=" & FParentID
                        .ExecuteNonQuery()

                    End If

                    '添加dsBsc
                    Dim mRow As DataRow
                    mRow = dsBSC.Tables("t_Account").NewRow
                    mRow("FAccountID") = FAccountID
                    mRow("FNumber") = FNumber
                    mRow("FName") = FName
                    mRow("FName_Cht") = FName_Cht
                    mRow("FLevel") = FLevel
                    mRow("FDetail") = IIf(FDetail = 1, "是", "否")
                    mRow("FParentID") = FParentID
                    mRow("FRootID") = FRootID
                    mRow("FGroupID") = Me.cmbFGroupID.Text
                    mRow("FDC") = Me.cmbFDC.Text
                    mRow("FHelperCode") = FHelperCode
                    mRow("FCurrencyID") = Me.cmbFCurrencyID.Text
                    mRow("FAdjustRate") = IIf(FAdjustRate = 1, "是", "否")
                    mRow("FQuantities") = IIf(FQuantities = 1, "是", "否")
                    mRow("FIsCash") = IIf(FIsCash = 1, "是", "否")
                    mRow("FIsBank") = IIf(FIsBank = 1, "是", "否")
                    mRow("FJournal") = IIf(FJournal = 1, "是", "否")
                    mRow("FContact") = IIf(FContact = 1, "是", "否")
                    mRow("FIsCashFlow") = IIf(FIsCashFlow = 1, "是", "否")
                    mRow("FCheckCase") = strFCheckCase
                    mRow("FIsBudget") = IIf(FIsBudget = 1, "是", "否")
                    mRow("FAcctint") = IIf(FAcctint = 1, "是", "否")
                    mRow("FintRate") = FintRate
                    mRow("FDelete") = "否"
                    mRow("FFullName") = FFullName
                    mRow("FGroupid2") = Microsoft.VisualBasic.Left(Me.cmbFGroupID.SelectedValue, 1)


                    dsBSC.Tables("t_Account").Rows.Add(mRow)
                    dsBSC.Tables("t_Account").AcceptChanges()

                    Me.tvAccount.Nodes.Clear()
                    LoadFirstAccount(tvAccount)
                    ListAccount("Account")

                End With


            Else

                With cmdSqlServer
                    .Connection = cnSqlServer
                    .Transaction = trnSqlServer
                    .CommandType = CommandType.Text

                    '2.更新PUR_POA插入記錄
                    .CommandText = "UPDATE t_Account SET " & _
                                          " FNumber='" & FNumber & _
                      "',FName='" & FName & _
                      "',FName_Cht='" & FName_Cht & _
                      "',FLevel=" & FLevel & _
                      ",FDetail=" & FDetail & _
                      ",FParentID=" & FParentID & _
                      ",FRootID=" & FRootID & _
                      ",FGroupID=" & FGroupID & _
                      ",FDC=" & FDC & _
                      ",FHelperCode='" & FHelperCode & _
                      "',FCurrencyID='" & FCurrencyID & _
                      "',FAdjustRate=" & FAdjustRate & _
                      ",FQuantities=" & FQuantities & _
                      ",FIsCash=" & FIsCash & _
                      ",FIsBank=" & FIsBank & _
                      ",FJournal=" & FJournal & _
                      ",FContact=" & FContact & _
                      ",FIsCashFlow=" & FIsCashFlow & _
                      ",FCheckCase='" & FCheckCase & _
                      "',FIsBudget=" & FIsBudget & _
                      ",FAcctint=" & FAcctint & _
                      ",FintRate=" & FintRate & _
                      ",FFullName='" & FFullName & _
                       "',LastUpdateDate=convert(varchar(19),getdate(),120)" & _
                      ",LastUpdateComputer=host_name()" & _
                      ",LastUpdateBy='" & PubUserName & "'" & _
                      " WHERE FNumber='" & strOriginalNum & "'"


                    .ExecuteNonQuery()

                    '修改dsBsc
                    Dim Rows() As DataRow = dsBSC.Tables("t_Account").Select("FNumber='" & strOriginalNum & "'")
                    If Rows.Length > 0 Then
                        For Each Row As DataRow In Rows
                            Row("FAccountID") = FAccountID
                            row("FNumber") = FNumber
                            row("FName") = FName
                            row("FName_Cht") = FName_Cht
                            row("FLevel") = FLevel
                            Row("FDetail") = IIf(FDetail = 1, "是", "否")
                            row("FParentID") = FParentID
                            row("FRootID") = FRootID
                            row("FGroupID") = Me.cmbFGroupID.Text
                            row("FDC") = Me.cmbFDC.Text
                            row("FHelperCode") = FHelperCode
                            row("FCurrencyID") = Me.cmbFCurrencyID.Text
                            row("FAdjustRate") = IIf(FAdjustRate = 1, "是", "否")
                            row("FQuantities") = IIf(FQuantities = 1, "是", "否")
                            row("FIsCash") = IIf(FIsCash = 1, "是", "否")
                            row("FIsBank") = IIf(FIsBank = 1, "是", "否")
                            row("FJournal") = IIf(FJournal = 1, "是", "否")
                            row("FContact") = IIf(FContact = 1, "是", "否")
                            row("FIsCashFlow") = IIf(FIsCashFlow = 1, "是", "否")
                            row("FCheckCase") = strFCheckCase
                            row("FIsBudget") = IIf(FIsBudget = 1, "是", "否")
                            row("FAcctint") = IIf(FAcctint = 1, "是", "否")
                            row("FintRate") = FintRate
                            row("FDelete") = "否"
                            row("FFullName") = FFullName
                            Row("FGroupid2") = Microsoft.VisualBasic.Left(Me.cmbFGroupID.SelectedValue, 1)


                            'dsBSC.Tables("t_Account").Rows.Add(Row)
                            dsBSC.Tables("t_Account").AcceptChanges()

                        Next
                    End If


                    Me.tvAccount.Nodes.Clear()
                    LoadFirstAccount(tvAccount)
                    ListAccount("Account")

                End With


            End If
            EnabledControls(False)

            trnSqlServer.Commit()


            MsgBox(IIf(ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd, My.Resources.AddSuccess, My.Resources.EditSuccess), MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
            ActionHelp = IDServer.IDServer.ActionBtn.ActionHelp

            Return True
        Catch ex As Exception
            trnSqlServer.Rollback()
            MsgBox("BtnActionSave:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
            Return False


        End Try
    End Function

    Private Sub TabControl1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
        Try
            If Me.TabControl1.SelectedIndex = 1 Then
                If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                    Exit Sub
                End If

                If dsBSC Is Nothing Then
                    Exit Sub
                End If

                If dsBSC.Tables.Count = 0 Then
                    Exit Sub
                End If

                If dsBSC.Tables("t_Account").Rows.Count = 0 Then
                    Exit Sub
                End If

                If lvAccount.SelectedItems.Count > 0 Then
                    If lvAccount.SelectedItems IsNot Nothing Then
                        Dim FNumber As String
                        FNumber = lvAccount.SelectedItems(0).Text
                        FillText(FNumber)
                        Me.TabControl1.SelectedIndex = 1

                    End If
                End If


            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        End Try
    End Sub

#End Region

#Region "Function Define"

    Private Sub GetPublic()
        Try

            Dim Table(5) As String
            Dim sql(5) As String

            '科目類型
            Table(0) = "t_AcctGroup"
            sql(0) = "SELECT * FROM t_AcctGroup "

            '會計科目
            Table(1) = "t_Account"
            sql(1) = "SELECT faccountid,fnumber,a.fname,a.fname_cht,flevel,fdetail=CASE fdetail WHEN 1 THEN '是' ELSE '否' END,fparentid," & _
                     " frootid,fgroupid=b.FName_Cht,fdc=CASE FDC WHEN 1 THEN '借' ELSE '貸' END,fhelpercode," & _
                     "fcurrencyid=CASE fcurrencyid WHEN (SELECT ISNULL(FValue,'HKD')  " & _
                     "  FROM dbo.[BSC_SystemProfile]  WHERE FCategory='System'  and Fkey='Standard money') " & _
                     " THEN '(不核算)' ELSE (select ListA from dbo.BSC_OtherDefineB " & _
                     " where ItemNo=(select Top 1 ID from dbo.BSC_OtherDefineA " & _
                     "  where ItemCode='CurrencyDefine') AND ListCode=a.fcurrencyid ) END " & _
                     " ,fadjustrate=CASE fadjustrate WHEN 1 THEN '是' ELSE '否' END," & _
                     " fquantities=CASE fquantities WHEN 1 THEN '是' ELSE '否' END," & _
                     " fiscash=CASE fiscash WHEN 1 THEN '是' ELSE '否' END," & _
                     "fisbank=CASE fisbank WHEN 1 THEN '是' ELSE '否' END," & _
                     "fjournal=CASE fjournal WHEN 1 THEN '是' ELSE '否' END," & _
                     "fcontact=CASE fcontact WHEN 1 THEN '是' ELSE '否' END," & _
                     "fiscashflow=CASE fiscashflow WHEN 1 THEN '是' ELSE '否' END," & _
                     "fcheckcase=dbo.GetFCheckCase(fcheckcase)," & _
                     "fisbudget=CASE fisbudget WHEN 1 THEN '是' ELSE '否' END," & _
                     "facctint=CASE facctint WHEN 1 THEN '是' ELSE '否' END," & _
                     "fintrate," & _
                     "fdelete=CASE fdelete WHEN 1 THEN '是' ELSE '否' END,ffullname," & _
                     "LEFT(a.fgroupid, 1) AS fgroupid2 " & _
                     "FROM t_account a,t_acctgroup b " & _
                     "WHERE a.fgroupid = b.fgroupid order by FNumber "

            '科目類型1
            Table(2) = "t_AcctGroup1"
            sql(2) = "SELECT '' FGroupID,'' FName_Cht UNION " & _
                     "SELECT FGroupID,FName_Cht FROM t_AcctGroup WHERE FGroupID<>0 " & _
                     "ORDER BY FGroupID "

            '外幣核算
            Table(3) = "FCurrencyid"
            sql(3) = "select ListCode,ListA=CASE ListCode WHEN (SELECT ISNULL(FValue,'HKD') ListCode " & _
                     "           FROM dbo.[BSC_SystemProfile]  WHERE FCategory='System'   " & _
                     " and Fkey='Standard money') THEN '(不核算)' ELSE ListA END " & _
                     " from BSC_OtherDefineB " & _
                     " where ItemNo=(select ID from BSC_OtherDefineA where ItemCode='CurrencyDefine') " & _
                     "ORDER BY ListA"

            '核算專案
            Table(4) = "FCheckCase"
            sql(4) = "select ListCode,ListA from dbo.BSC_OtherDefineB " & _
                     "where ItemNo=(select Top 1 ID from dbo.BSC_OtherDefineA where ItemCode='FCheckCase') " & _
                     "order by ListCode "

            '核算專案
            Table(5) = "System"
            sql(5) = "SELECT Isnull(FValue,'-') FValue  FROM dbo.[BSC_SystemProfile] WHERE Fcategory='System' and FKey='Account separating character' "

            dsBSC = GetDataSet(sql, Table)

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 添加1級TreeNode
    ''' </summary>
    ''' <param name="TV">TreeView</param>
    ''' <remarks></remarks>
    Private Sub LoadFirstAccount(ByVal TV As TreeView)
        Try
            '載入圖像
            Dim Node As New TreeNode
            Img = New ImageList()
            Dim Image As Image = Nothing
            Img.Images.Add(Drawing.Image.FromFile(appPath & "CLSDFOLD.BMP"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "OPENFOLD.BMP"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "assets.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "costs.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "Delete.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "equity.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "Export.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "FDelete.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "liabilities.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "Manger.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "new.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "other.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "period.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "print.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "priv.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "prop.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "refresh.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "UnFDelete.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "001.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "002.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "003.bmp"))
            Img.Images.Add(Drawing.Image.FromFile(appPath & "004.bmp"))


            TV.ImageList = Img
            Node.Nodes.Clear()
            Node.Tag = "Account"
            Node.Text = "科目"
            Node.ImageIndex = 0
            TV.Nodes.Add(Node)
            SelectNode = Node

            '添加會計科目類別
            Dim Rows() As DataRow = dsBSC.Tables("t_AcctGroup").Select("FGroupID=0")
            Dim GroupID As Int16
            Dim strFilter As String
            For I As Integer = 0 To Rows.Length - 1
                Dim newNode As New TreeNode
                With newNode
                    .Tag = Rows(I).Item("FClassID").ToString & "^" & Rows(I).Item("FName_Cht").ToString
                    .Text = Rows(I).Item("FName_Cht")
                    GroupID = Rows(I).Item("FClassID")
                    Select Case Rows(I).Item("FName_Cht").ToString
                        Case "資產", "營業收入", "營業外收入"
                            .ImageIndex = 2
                            .SelectedImageIndex = 2
                        Case "負債", "營業外支出"
                            .ImageIndex = 8
                            .SelectedImageIndex = 8
                        Case "權益", "股東權益"
                            .ImageIndex = 5
                            .SelectedImageIndex = 5
                        Case "成本", "營業成本", ".營業費用"
                            .ImageIndex = 3
                            .SelectedImageIndex = 3
                        Case "損益"
                            .ImageIndex = 12
                            .SelectedImageIndex = 12
                        Case Else
                            .ImageIndex = 11
                            .SelectedImageIndex = 11


                    End Select
                    Node.Nodes.Add(newNode)

                    strFilter = "FGroupID2='" & GroupID & "'"
                    '添加明細科目
                    If dsAccount IsNot Nothing Then
                        dsAccount.Tables.Clear()
                        Dim tb As New DataTable
                        tb = dsBSC.Tables("t_Account").Clone()

                        Dim mRows() As DataRow
                        mRows = dsBSC.Tables("t_Account").Select(strFilter)

                        For J As Integer = 0 To mRows.Length - 1
                            Dim mRow As DataRow = tb.NewRow
                            For K As Integer = 0 To tb.Columns.Count - 1
                                mRow(K) = mRows(J).Item(K)

                            Next
                            tb.Rows.Add(mRow)
                        Next
                        dsAccount.Tables.Add(tb)
                        dsAccount.AcceptChanges()
                        LoadSecondTree(newNode, 0)
                    End If

                End With
            Next
            Node.Expand()

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 添加下級TreeNode
    ''' </summary>
    ''' <param name="TreeNode">TreeNode</param>
    ''' <param name="ParentID">父ID</param>
    ''' <remarks></remarks>
    Private Sub LoadSecondTree(ByVal TreeNode As TreeNode, ByVal ParentID As Integer)
        Try
            If dsAccount Is Nothing Then Exit Sub
            Dim Rows() As DataRow = dsAccount.Tables(0).Select("FParentID=" & ParentID)
            Dim Define As Int16 = 0
            For Each mRow As DataRow In Rows
                Dim mNode As New TreeNode
                Define = mRow("FLevel")
                If TreeNode Is Nothing Then
                    mNode.Tag = mRow("FAccountID") & "^" & mRow("FLevel")
                    mNode.Text = "(" & mRow("FNumber") & ")" & mRow("FName_Cht")
                    mNode.ImageIndex = 0
                    mNode.SelectedImageIndex = 1
                    TreeNode.Nodes.Add(mNode)
                    mNode.Expand()
                    LoadSecondTree(mNode, mRow("FAccountID"))
                Else
                    mNode.Tag = mRow("FAccountID") & "^" & mRow("FLevel")
                    mNode.Text = "(" & mRow("FNumber") & ")" & mRow("FName_Cht")
                    mNode.ImageIndex = 0
                    mNode.SelectedImageIndex = 1
                    TreeNode.Nodes.Add(mNode)
                    mNode.Expand()
                    LoadSecondTree(mNode, mRow("FAccountID"))
                End If
            Next
        Catch ex As Exception
            MsgBox("LoadSecondTree:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub LoadlvColumns(ByVal lv As ListView)
        Try

            lv.Columns.Add("科目代碼", 80, HorizontalAlignment.Center)
            lv.Columns.Add("科目名稱", 150, HorizontalAlignment.Left)
            lv.Columns.Add("科目名稱(中文)", 150, HorizontalAlignment.Left)
            lv.Columns.Add("借貸方向", 70, HorizontalAlignment.Center)
            lv.Columns.Add("助記碼", 80, HorizontalAlignment.Left)
            lv.Columns.Add("是否禁用", 70, HorizontalAlignment.Center)
            lv.Columns.Add("全名", 200, HorizontalAlignment.Left)
            lv.Columns.Add("科目類別", 200, HorizontalAlignment.Left)
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub ListAccount(ByVal strTag As String)
        Try
            Me.lvAccount.BeginUpdate()
            Me.lvAccount.Items.Clear()

            Dim Rows() As DataRow = dsBSC.Tables("t_AcctGroup").Select("FGroupID=0")

            '添加明細科目
            If dsAccount IsNot Nothing Then
                dsAccount.Tables.Clear()
                Dim tb As New DataTable

                '清除非本類別的會計科目
                tb = dsBSC.Tables("t_Account").Copy()
                If strTag <> "Account" Then
                    For Each row As DataRow In tb.Rows
                        If row.Item("FNumber").ToString.Length < Len(strTag) Then
                            row.Delete()
                        Else
                            If row.Item("FNumber").ToString.Substring(0, Len(strTag)) <> strTag Then
                                row.Delete()
                            End If
                        End If

                    Next

                End If
                dsAccount.Tables.Add(tb)
                dsAccount.AcceptChanges()
            End If

            For I As Integer = 0 To dsAccount.Tables(0).Rows.Count - 1
                Dim item1 As New ListViewItem
                item1.Text = dsAccount.Tables(0).Rows(I).Item("FNumber")
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FName") & "")
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FName_Cht"))
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FDC"))
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FHelperCode") & "")
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FDelete") & "")
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FFullName"))
                item1.SubItems.Add(dsAccount.Tables(0).Rows(I).Item("FGroupID"))

                If dsAccount.Tables(0).Rows(I).Item("FDelete") = "是" Then
                    item1.ForeColor = Color.Red
                Else
                    item1.ForeColor = Color.Black
                End If

                Me.lvAccount.Items.Add(item1)

            Next

            Me.lvAccount.EndUpdate()


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 重新綁定控制項的資料源
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ReBindControls()
        Try
            GetPublic()

            '綁定數據

            With Me.cmbFGroupID
                .DataSource = dsBSC.Tables("t_AcctGroup1")
                .ValueMember = ("FGroupID")
                .DisplayMember = ("FName_Cht")
            End With

            With Me.cmbFCurrencyID
                .DataSource = dsBSC.Tables("FCurrencyid")
                .ValueMember = ("ListCode")
                .DisplayMember = ("ListA")
            End With

            Me.lvFCheckCaseAll.BeginUpdate()
            Me.lvFCheckCaseAll.Items.Clear()

            For I As Integer = 0 To dsBSC.Tables("FCheckCase").Rows.Count - 1
                Dim item1 As New ListViewItem
                item1.Text = dsBSC.Tables("FCheckCase").Rows(I).Item("ListA")
                item1.Tag = dsBSC.Tables("FCheckCase").Rows(I).Item("ListCode")

                Me.lvFCheckCaseAll.Items.Add(item1)

            Next

            Me.lvFCheckCaseAll.EndUpdate()
            CheckItems()

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try


    End Sub

    Private Sub CheckItems()
        Try
            If Me.lvFCheckCaseAll.Items.Count > 0 Then
                Me.btnSelect.Enabled = True
                Me.btnSelectAll.Enabled = True
            Else
                Me.btnSelect.Enabled = False
                Me.btnSelectAll.Enabled = False
            End If

            If Me.lvFCheckCase.Items.Count > 0 Then
                Me.btnClear.Enabled = True
                Me.btnClearAll.Enabled = True
            Else
                Me.btnClear.Enabled = False
                Me.btnClearAll.Enabled = False
            End If

        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' ClearText()
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ClearText()
        Try
            Me.TxtFNumber.Text = ""
            Me.TxtFHelperCode.Text = ""
            Me.cmbFDC.SelectedIndex = 0
            Me.txtFName.Text = ""
            Me.cmbFGroupID.SelectedIndex = 0
            Me.txtFName_Cht.Text = ""
            Me.cmbFCurrencyID.SelectedIndex = 0
            Me.chkFAcctint.Checked = False
            Me.chkFAdjustRate.Checked = False
            Me.chkFIsBank.Checked = False
            Me.chkFIsBudget.Checked = False
            Me.chkFIsCash.Checked = False
            Me.chkFIsCashFlow.Checked = False
            Me.chkFJournal.Checked = False
            Me.chkFQuantities.Checked = False
            Me.chkFContact.Checked = False

            Me.nudFintRate.Value = 0

            Me.btnClearAll_Click(Nothing, Nothing)


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 設置控件的Enable屬性
    ''' </summary>
    ''' <param name="B"></param>
    ''' <remarks></remarks>
    Private Sub EnabledControls(ByVal B As Boolean)
        Me.GroupBox1.Enabled = B

    End Sub

    ''' <summary>
    ''' 判斷是否有記錄
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function FindRecord() As Boolean

        If dsBSC Is Nothing Then
            Return False
            Exit Function
        ElseIf dsBSC.Tables("t_Account").Rows.Count = 0 Then
            Return False
            Exit Function
        Else
            Return True

        End If

    End Function

    ''' <summary>
    ''' 設置按鈕狀態
    ''' </summary>
    ''' <param name="type"></param>
    ''' <remarks></remarks>
    Private Sub SetButton(ByVal type As ButtonType, ByVal IsFind As Boolean)
        Try

            Select Case type
                Case ButtonType.Add
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Browse
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Cancel
                    With MainFrom
                        .ToolStripAdd.Enabled = True
                        .ToolStripEdit.Enabled = IsFind
                        .ToolStripDelete.Enabled = IsFind
                        .ToolStripSave.Enabled = False
                        .ToolStripCancel.Enabled = False
                        .ToolStripFirst.Enabled = IsFind
                        .ToolStripLast.Enabled = IsFind
                        .ToolStripNext.Enabled = IsFind
                        .ToolStripPrivous.Enabled = IsFind
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

                Case ButtonType.Edit
                    With MainFrom
                        .ToolStripAdd.Enabled = False
                        .ToolStripEdit.Enabled = False
                        .ToolStripDelete.Enabled = False
                        .ToolStripSave.Enabled = True
                        .ToolStripCancel.Enabled = True
                        .ToolStripFirst.Enabled = False
                        .ToolStripLast.Enabled = False
                        .ToolStripNext.Enabled = False
                        .ToolStripPrivous.Enabled = False
                        .ToolStripExport.Enabled = IsFind
                        .ToolStripImport.Enabled = True
                        .ToolStripPrint.Enabled = IsFind
                        .ToolStripFilter.Enabled = True

                    End With

            End Select
        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try

    End Sub

    ''' <summary>
    ''' 
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub GetToolEnable()
        Try
            With MainFrom
                .ToolStripAdd.Enabled = AddEnable
                .ToolStripEdit.Enabled = EditEnable And bFind
                .ToolStripDelete.Enabled = DeleteEnable And bFind
                .ToolStripPrint.Enabled = PrintEnable
                .ToolStripImport.Enabled = ImportEnable
                .ToolStripExport.Enabled = ExportEnable
                .ToolStripFind.Enabled = FindEnable
                .ToolStripCheck.Enabled = CheckEnable
                .ToolStripUnCheck.Enabled = UncheckEnable
                .ToolStripComplete.Enabled = CompleteEnable
                .AddToolStripMenuItem.Enabled = .ToolStripAdd.Enabled
                .Edit1ToolStripMenuItem.Enabled = .ToolStripEdit.Enabled
                .DelToolStripMenuItem.Enabled = .ToolStripDelete.Enabled
                .SaveToolStripMenuItem.Enabled = .ToolStripSave.Enabled
                .CancelToolStripMenuItem.Enabled = .ToolStripCancel.Enabled
                .PrintToolStripMenuItem.Enabled = .ToolStripPrint.Enabled
                .ExportToolStripMenuItem.Enabled = .ToolStripImport.Enabled
                .ImportToolStripMenuItem.Enabled = .ToolStripExport.Enabled
                .FilterToolStripMenuItem.Enabled = .ToolStripFilter.Enabled
            End With

        Catch ex As Exception
            MsgBox("GetToolEnable:" + ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    '從DataGridView選定記錄後填充Text
    Private Sub FillText(ByVal Num As String)
        Try
            ClearText()

            Dim mRow() As DataRow = dsBSC.Tables("t_Account").Select("FNumber='" & Num & "'")
            If mRow Is Nothing Then Exit Sub
            For Each Row As DataRow In mRow
                Me.TxtFNumber.Text = Row("FNumber") & ""
                Me.TxtFHelperCode.Text = Row("FHelperCode") & ""
                Me.txtFName.Text = Row("FName") & ""
                Me.txtFName_Cht.Text = Row("FName_Cht") & ""
                Me.cmbFDC.Text = Row("FDC") & ""
                Me.cmbFGroupID.Text = Row("FGroupID") & ""
                Me.cmbFCurrencyID.Text = Row("fcurrencyid") & ""
                Me.chkFAcctint.Checked = IIf(Row("FAcctint") = "是", True, False)
                Me.chkFAdjustRate.Checked = IIf(Row("FAdjustRate") = "是", True, False)
                Me.chkFIsBank.Checked = IIf(Row("FIsBank") = "是", True, False)
                Me.chkFIsBudget.Checked = IIf(Row("FIsBudget") = "是", True, False)
                Me.chkFIsCash.Checked = IIf(Row("FIsCash") = "是", True, False)
                Me.chkFIsCashFlow.Checked = IIf(Row("FIsCashFlow") = "是", True, False)
                Me.chkFJournal.Checked = IIf(Row("FJournal") = "是", True, False)
                Me.chkFQuantities.Checked = IIf(Row("FQuantities") = "是", True, False)
                Me.chkFContact.Checked = IIf(Row("FContact") = "是", True, False)

                Me.nudFintRate.Value = Row("FintRate")

                '添加核算專案
                If Row("FCheckCase") <> "" Then
                    Dim str() As String = Row("FCheckCase").ToString.Split(",")
                    Dim FNumber As String = ""
                    For I As Integer = 0 To str.Length - 1
                        FNumber = str(I)
                        For J As Integer = Me.lvFCheckCaseAll.Items.Count - 1 To 0 Step -1
                            If Me.lvFCheckCaseAll.Items(J).Text = FNumber Then
                                Me.lvFCheckCase.Items.Add(Me.lvFCheckCaseAll.Items(J).Clone)
                                Me.lvFCheckCaseAll.Items(J).Remove()
                            End If
                        Next
                    Next


                End If


            Next


        Catch ex As Exception
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    ''' <summary>
    ''' 過濾符合條件的記錄
    ''' </summary>
    ''' <remarks></remarks>
    Sub Filter()
        Try
            If ActionHelp = IDServer.IDServer.ActionBtn.ActionAdd Or ActionHelp = IDServer.IDServer.ActionBtn.ActionEdit Then
                Exit Sub
            End If

            Me.Cursor = Cursors.WaitCursor

            ReBindControls()

            Me.tvAccount.Nodes.Clear()
            LoadFirstAccount(tvAccount)
            ListAccount("Account")


            IsFind = FindRecord()

            bFind = IsFind
            SetButton(ButtonType.Browse, IsFind)
            GetToolEnable()

        Catch ex As Exception
            Me.Cursor = Cursors.Default
            MsgBox(ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)

        Finally
            Me.Cursor = Cursors.Default
        End Try


    End Sub

#End Region


    Private Sub Label9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LBLDailyAccrual.Click

    End Sub
End Class
